/*
 * Copyright (c) 2009 Tim Mead
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 */

/**
 * Jump to coordinate command. This is used to display a dialog, allowing
 * the user to enter a latitude and longitude pair to centre the view on.
 */
package com.misoneism.fire.commands
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.misoneism.fire.model.ModelLocator;
	import com.misoneism.fire.util.DialogEvent;
	import com.misoneism.fire.util.DialogUtil;
	import com.misoneism.fire.views.JumpToCoordinate;
	
	import flash.display.DisplayObject;
	
	import mx.core.Application;
	
	public class JumpToCoordinateCommand
			implements ICommand
	{
		[Embed(source='../images/jump-coordinate-16.png')]
		private var icon:Class;
		
		/**
		 * Process the event. Here we display a dialog to the user.
		 */
		public function execute(event:CairngormEvent):void
		{
			DialogUtil.showDialog(Application.application as DisplayObject,
				new JumpToCoordinate(), "Jump to Coordinate",
				true, DialogUtil.BUTTON_OK | DialogUtil.BUTTON_CANCEL | DialogUtil.ICON_QUESTION, icon,
				dialogCallback);
		}
		
		/**
		 * Callback for the dialog closing: if the user accepted the dialog,
		 * then tell the map to display the specified coordinate.
		 */
		protected function dialogCallback(event:DialogEvent):void
		{
			if (event.button == DialogUtil.BUTTON_OK)
			{
				var content:JumpToCoordinate = event.source as JumpToCoordinate;
				
				ModelLocator.getInstance().mapDisplay.displayCoordinate(content.latitude.text,
					content.longitude.text);
			}
		}
	}
}